var express = require("express");
var app = express();
var bodyParser = require("body-parser")

app.use(
    bodyParser.urlencoded({
        extended: true
    }));
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.all("*", (req, res, next) => {
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Headers", "*");
    res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.setHeader("Access-Control-Allow-Credentials", "true")
    next()
});

const adminLogin = require("./data/admin_login.json")
const workGroupLogin = require("./data/workGroup_login.json")

const adminPermission = require("./data/admin.json")
const engineerPermission = require("./data/工程部.json")
const supplyPermission = require("./data/物资部.json")
const kuguanPermission = require("./data/库管.json")
const workGroupPermission = require("./data/作业小组.json")
const workTeamPermission = require("./data/作业队.json")

const partTable = require("./data/零件表1.json")
const partTable2 = require("./data/零件表2.json")
const partName = require("./data/零件名称表.json")
const partSpe = require("./data/零件规格表.json")

/* 登录 */
app.post('/user/login', (req, res) => {
    let { account, password } = req.body;
    if (account == 'admin' && password == 123456) {
        res.send(adminLogin)
    }
    else if (account == '作业队技术员' && password == 123456) {
        res.send(workGroupLogin)
    }
    else {
        res.send('ERROR')
    }
})

/* 权限获取 */
app.get("/routing/admin", (req, res) => {
    res.send(adminPermission)
})
app.get("/routing/engineering", (req, res) => {
    res.send(engineerPermission)
})
app.get("/routing/supplies", (req, res) => {
    res.send(supplyPermission)
})
app.get("/routing/treasury", (req, res) => {
    res.send(kuguanPermission)
})
app.get("/routing/workGroup", (req, res) => {
    res.send(workGroupPermission)
})
app.get("/routing/workTeam", (req, res) => {
    res.send(workTeamPermission)
})

/* 零件数据获取 */
app.post("/material/conditionSearch/:current", (req, res) => {
    let data = req.body;
    let { current } = req.params;
    if (isObjEqual(data, {})) {
        if (current == 1) {
            res.send(partTable)
        } else if (current == 2) {
            res.send(partTable2)
        } else {
            res.send({
                "success": false,
                "code": 400,
                "message": "失败，当前页没有数据",
                "data": {}
            })
        }
    }
    else {
        res.send("HCYSB")
    }


})

/* 零件名称获取 */
app.get("/material/name", (req, res) => {
    res.send(partName)
})

/* 零件规格获取 */
app.get("/material/specification", (req, res) => {
    res.send(partSpe)
})

function isObjEqual(o1, o2) {
    var props1 = Object.getOwnPropertyNames(o1);
    var props2 = Object.getOwnPropertyNames(o2);
    if (props1.length != props2.length) {
        return false;
    }
    for (var i = 0, max = props1.length; i < max; i++) {
        var propName = props1[i];
        if (o1[propName] !== o2[propName]) {
            return false;
        }
    }
    return true;
}



app.listen(4040, '0.0.0.0', () => {
    console.log("http://localhost:4040");
})